/**
 * Copyright(c) kyle.
 */
package org.example.tomcat.netty.basic;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.QueryStringDecoder;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * 请求
 * 用字符串切割的方法提取请求方式和url
 *
 * @author kyle
 * @version 1.00.00
 * @since [2020-10-20 18:20]
 * @updateUser [Kyle]
 * @updateRemark [说明本次修改内容]
 * @date [2020-10-20 18:20]
 */
public class GPRequest {

    private ChannelHandlerContext ctx;
    private HttpRequest req;

    public GPRequest(final ChannelHandlerContext pCtx, final HttpRequest pReq) {
        ctx = pCtx;
        req = pReq;
    }

    public String getUrl(){
        return req.uri();
    }
    public String getMethod(){
        return req.method().name();
    }
    public Map<String , List<String>> getParameters(){
        QueryStringDecoder decoder=new QueryStringDecoder(req.uri());
        return decoder.parameters();
    }
    public String getParameter(String name){
        Map<String,List<String>> params=getParameters();
        List<String> param=params.get(name);
        if(null==param){
            return null;
        }else {
            return param.get(0);
        }
    }
}
